package com.ruoyi.teaching.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.teacher.domain.MyTask;
import com.ruoyi.teacher.domain.Teacher;
import com.ruoyi.teacher.mapper.MyTaskMapper;
import com.ruoyi.teacher.mapper.TeacherMapper;
import com.ruoyi.teaching.dto.TaskupdateDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.teaching.mapper.TeacherStalkeMapper;
import com.ruoyi.teaching.domain.TeacherStalke;
import com.ruoyi.teaching.service.ITeacherStalkeService;

/**
 * 老师跟踪
Service业务层处理
 * lbj
 * @author ruoyi
 * @date 2023-05-18
 */
@Service
public class TeacherStalkeServiceImpl implements ITeacherStalkeService 
{
    @Autowired
    private TeacherStalkeMapper teacherStalkeMapper;

    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private MyTaskMapper myTaskMapper;
    /**
     * 查询老师跟踪

     * 
     * @param id 老师跟踪
主键
     * @return 老师跟踪

     */
    @Override
    public TeacherStalke selectTeacherStalkeById(Long id)
    {
        return teacherStalkeMapper.selectTeacherStalkeById(id);
    }

    /**
     * 查询老师跟踪
列表
     * 
     * @param teacherStalke 老师跟踪

     * @return 老师跟踪

     */
    @Override
    public List<TeacherStalke> selectTeacherStalkeList(TeacherStalke teacherStalke)
    {
        List<TeacherStalke> list = teacherStalkeMapper.selectTeacherStalkeList(teacherStalke);
        return teacherStalkeMapper.selectTeacherStalkeList(teacherStalke);
    }

    /**
     * 新增老师跟踪

     * 
     * @param teacherStalke 老师跟踪

     * @return 结果
     */
    @Override
    public int insertTeacherStalke(TeacherStalke teacherStalke)
    {
        List<TeacherStalke> stalke = teacherStalkeMapper.findByStudentId(teacherStalke.getStudentId());
        LoginUser user = SecurityUtils.getLoginUser();
        String name = user.getUsername();
        // 如果该学生不是第一次跟踪那么就在跟踪上面加一
        Long i = 0l;
        if (stalke.size() < 1) {
            teacherStalke.setTeachername(name);
            teacherStalke.setStalkeCount(i+1);
            extracted(teacherStalke, user);
            return teacherStalkeMapper.insertTeacherStalke(teacherStalke);
        }
        // 不是第一次跟踪
        extracted(teacherStalke, user);
        // 拿到之前记录的跟踪次数
        TeacherStalke teacherStalke1 = stalke.get(0);
        teacherStalke.setStalkeCount(teacherStalke1.getStalkeCount());
        teacherStalke.setTeachername(name);
        // 添加最新的一次跟踪记录
        teacherStalkeMapper.insertTeacherStalke(teacherStalke);
         stalke.add(teacherStalke);
         // 遍历集合重新改变跟踪次数
        for (TeacherStalke sta: stalke
             ) {
            Long count = sta.getStalkeCount();
            sta.setStalkeCount(count+1);
        }

        return teacherStalkeMapper.updateList(stalke);

    }

    // 添加数据库和setteanchId
    private void extracted(TeacherStalke teacherStalke, LoginUser user) {
        // 该学生第一次跟踪
        String email = user.getUser().getEmail();
        Teacher teacher =  teacherMapper.fingByEmail(email);
        Teacher teacher1 = new Teacher();
        if (teacher == null) {
            teacher1.setName(user.getUsername());
                teacher1.setEmail(user.getUser().getEmail());
            teacherMapper.insertTeacher(teacher1);
            Long id = teacher1.getId();
            teacherStalke.setTeacherId(id);
        } else {
            Long ids = teacher.getId();
            teacherStalke.setTeacherId(ids);
        }
        teacherStalkeMapper.updateTeacherStalke(teacherStalke);
    }

    /**
     * 修改老师跟踪

     * 
     * @param teacherStalke 老师跟踪

     * @return 结果
     */
    @Override
    public int updateTeacherStalke(TeacherStalke teacherStalke)
    {
        return teacherStalkeMapper.updateTeacherStalke(teacherStalke);
    }

    /**
     * 批量删除老师跟踪

     * 
     * @param ids 需要删除的老师跟踪
主键
     * @return 结果
     */
    @Override
    public int deleteTeacherStalkeByIds(Long[] ids)
    {
        return teacherStalkeMapper.deleteTeacherStalkeByIds(ids);
    }

    /**
     * 删除老师跟踪
信息
     * 
     * @param id 老师跟踪
主键
     * @return 结果
     */
    @Override
    public int deleteTeacherStalkeById(Long id)
    {
        return teacherStalkeMapper.deleteTeacherStalkeById(id);
    }

    @Override
    public List<TeacherStalke> findByStudentId(Long studentId) {
        return teacherStalkeMapper.findByStudentId(studentId);
    }

    @Override
    public List<TeacherStalke> getfindListByStudentId(Long studentId) {
        List<TeacherStalke> stalkes= teacherStalkeMapper.getfindListByStudentId(studentId);
        return stalkes;
    }

    @Override
    public List<MyTask> getfindListmodifyHistorys(String name) {
        List<MyTask> myTask =  teacherStalkeMapper.getfindListmodifyHistorys(name);
        return myTask;
    }

    @Override
    public int gettaskupdate(TaskupdateDto dto) {
        Long id = dto.getMytaskId();
        MyTask myTask = myTaskMapper.selectMyTaskById(id);
        myTask.setFinish(dto.getFinish());
        myTask.setFinishContent(dto.getFinishContent());
        return myTaskMapper.updateMyTask(myTask);
    }
}
